# import cv2
# import numpy as np

# # 读取图像
# frame = cv2.imread('/home/cat/Src/img/output.png')

# hsv_image = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)

# # 定义目标颜色范围（这里针对浅绿色进行调整）
# lower_green = np.array([73, 111, 120])
# upper_green = np.array([81, 147, 140])


# # 创建颜色掩码
# mask = cv2.inRange(hsv_image, lower_green, upper_green)

# # 对原图和掩码做位运算获取彩色部分
# # color_components = cv2.bitwise_and(frame, frame, mask=mask)
# kernel = np.ones((5, 5), np.uint8)  
# mask = cv2.morphologyEx(mask, cv2.MORPH_CLOSE, kernel)  

# # 转为灰度图并进行阈值处理以获取二值图像
# # gray_mask = cv2.cvtColor(color_components, cv2.COLOR_BGR2GRAY)
# # _, binary_mask = cv2.threshold(gray_mask, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)

# # 寻找轮廓
# contours, hierarchy = cv2.findContours(mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)


# for cnt in contours:
#     # 计算轮廓的边界框（最小外接矩形）
#     x,y,w,h = cv2.boundingRect(cnt)
    
#     # 画出边界框
#     cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 255, 0), 2)

    
#     # 提取轮廓区域
#     roi = mask[y:y+h, x:x+w]
#     contour_area = cv2.contourArea(cnt)
    
#     # 根据轮廓特性判断形状
#     # if cv2.isContourConvex(cnt):
#     #     # 圆形识别（可以使用拟合圆形的方法HoughCircles或minEnclosingCircle）
#     #     (center), radius = cv2.minEnclosingCircle(cnt)
#     #     center = (int(center[0]), int(center[1]))
        
#     #     # 只有当半径在合理范围内才认为是圆形
#     #     if radius > 10 and radius < 100:  # 调整这个范围以适应你的七巧板圆的大小
#     #         cv2.circle(frame, (int(center[0] + x), int(center[1] + y)), int(radius), (0, 255, 0), 2)
#     #         # self.position["circle"] = [center[0],center[1],0]
#     #         print(f"圆形中心坐标：({center[0]}, {center[1]}")

#     #     # 其他形状识别主要通过边数和角度等特征判断
#     #     else:
#     approx = cv2.approxPolyDP(cnt, 0.01 * cv2.arcLength(cnt, True), True)
#     n_corners = len(approx)
    
#     if n_corners == 3:  # 三角形
#         M = cv2.moments(cnt)
#         center_tri = (int(M["m10"] / M["m00"]), int(M["m01"] / M["m00"]))
#         cv2.drawContours(frame, [approx], -1, (0, 255, 0), 2)
#         print(f"三角形中心坐标：({center_tri[0]}, {center_tri[1]}")

#     elif n_corners == 4:  # 正方形/矩形
#         if abs(cv2.contourArea(cnt) / ((w*h) ** 0.5)) > 0.95:  # 基于面积对角线比识别正方形
#             M = cv2.moments(cnt)
#             center_square = (int(M["m10"] / M["m00"]), int(M["m01"] / M["m00"]))
#             cv2.drawContours(frame, [approx], -1, (0, 255, 0), 2)
#             print(f"正方形中心坐标：({center_square[0]}, {center_square[1]}")

#     elif n_corners == 6:  # 六边形
#         M = cv2.moments(cnt)
#         center_hexagon = (int(M["m10"] / M["m00"]), int(M["m01"] / M["m00"]))
#         cv2.drawContours(frame, [approx], -1, (0, 255, 0), 2)
#         print(f"六边形中心坐标：({center_hexagon[0]}, {center_hexagon[1]}")
    
#     else:
#         M = cv2.moments(cnt)
#         center = (int(M["m10"] / M["m00"]), int(M["m01"] / M["m00"]))
#         cv2.drawContours(frame, [approx], -1, (0, 255, 0), 2)
#         print(f"圆形中心坐标：({center[0]}, {center[1]}")
        

# # 遍历轮廓并找到最大的绿色圆形
# max_area = 0
# max_contour = None
# for cnt in contours:
#     area = cv2.contourArea(cnt)
#     if area > max_area and cv2.isContourConvex(cnt):
#         max_area = area
#         max_contour = cnt

# # 如果找到了绿色圆形，则绘制其外接矩形
# if max_contour is not None:
#     rect = cv2.minAreaRect(max_contour)
#     box = cv2.boxPoints(rect)
#     box = np.int0(box)
#     cv2.drawContours(img, [box], 0, (0, 255, 0), 2)

# 显示结果
# cv2.imwrite('Result.png', img)
# cv2.waitKey(0)
# cv2.destroyAllWindows()


import cv2
import numpy as np

# 读取图片
img = cv2.imread('/home/cat/Src/img/output.png')

# 将图片转换为HSV色彩空间
hsv_img = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)

# 定义你要查看的区域（例如一个矩形区域）
x_start, y_start = 237, 116  # 区域左上角坐标
width, height = 90, 100  # 区域的宽度和高度
x_end = x_start + width
y_end = y_start + height

# 提取该区域的HSV值
roi = hsv_img[y_start:y_end, x_start:x_end]

# 获取HSV三个通道各自的最大值和最小值
h_min, s_min, v_min = np.min(roi, axis=(0, 1))
h_max, s_max, v_max = np.max(roi, axis=(0, 1))

print(f"HSV区域最值：H_min={h_min}, S_min={s_min}, V_min={v_min}")
print(f"H_max={h_max}, S_max={s_max}, V_max={v_max}")